package parser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jdom.Attribute;

public class Parser {
	
	protected static Map<String, String> attRestricao = new HashMap<String, String>();
	static {
		attRestricao.put("*", "obrigatorio");
		attRestricao.put("+", "pelo_menos_um");
		attRestricao.put("opcional", "opcional");
	}
	
	String string = "";
	
	public Parser() 
	{
	}
	
	
	public static String[] split(String in)
	{
		Pattern p = Pattern.compile("[*+]"); 
		Matcher m = p.matcher(in);
		
		int endStr = in.indexOf("(") > 0 ? in.indexOf("(") : in.length();
		
		boolean encontrou = m.find(); 
		
		String a = encontrou ? in.substring(0,1) : "opcional";
		String b = encontrou ? in.substring(1, endStr): in.substring(0, endStr);
		
		return new String[]{ a, b };
	}
	
	
	public static Attribute getRestricaoAttribute(String token)
	{
		return new Attribute("restricao", attRestricao.get(token)); 
	}
	

	public static List<Attribute> getAttributes(String token)
	{
		List<Attribute> list = new ArrayList<Attribute>();
		String[] split = split(token);
		list.add(new Attribute("restricao", attRestricao.get(split[0])));
		list.add(new Attribute("nome", split[1]));
		return list; 
	}

}
